import 'package:app/core/theme/app_style.dart';
import 'package:app/page/chat/entity/message.dart';
import 'package:bubble/bubble.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class MessageItem extends StatelessWidget {
  final Message entity;

  const MessageItem(this.entity, {Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Bubble(
      style: entity.isSelf ? styleMe : styleSomebody,
      child: buildItem(entity.type),
    );
  }

  buildItem(String type) {
    switch (type) {
      case 'msg':
        return Text(entity.msg);
        break;
      case 'img':
        // TODO 网址校验 size协调
        return SizedBox(
            width: 120.w,
            height: 180.h,
            child: CachedNetworkImage(
                imageUrl: entity.msg,
                placeholder: (context, url) => CircularProgressIndicator(),
                errorWidget: (context, url, error) => ClipOval(
                        child: Image(
                      image: AssetImage('icons/cat.jpeg'),
                    ))));
        break;
      case 'file':
        break;
    }
  }
}
